package com.key.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.key.common.base.RespResult;
import com.key.common.constants.IndentState;
import com.key.common.utils.OddNumbersUtils;
import com.key.common.utils.StringIsNotNull;
import com.key.modules.dto.IndentDTO;
import com.key.modules.entity.ECustomers;
import com.key.modules.entity.ICustomers;
import com.key.modules.entity.OrderForm;
import com.key.modules.mapper.ECustomersMapper;
import com.key.modules.mapper.ICustomersMapper;
import com.key.modules.mapper.IndentMapper;
import com.key.modules.service.IndentService;
import com.key.modules.vo.IndentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class IndentServiceImpl implements IndentService {

    @Autowired
    private IndentMapper indentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ICustomersMapper iCustomersMapper;

    @Autowired
    private ECustomersMapper eCustomersMapper;


    @Override
    public RespResult showIndentByDTO(IndentDTO indentDTO) {
        log.info("进入查询方法");
        Page<IndentVO> page=new Page<>(indentDTO.getCurrent(),indentDTO.getSize());
        Page<IndentVO> indentVOPage = indentMapper.selectIndentByDTO(page, indentDTO);
        List<IndentVO> records = indentVOPage.getRecords();
        redisTemplate.opsForHash().put("订单列表","订单",records);



        log.info("查询结果="+records);
        return RespResult.success(records);
    }

    @Override
    public RespResult addIndent(OrderForm orderForm) {
        log.info("进入添加方法，参数为："+orderForm);

//        log.info("判断客户是否存在"+orderForm.getCustomerName());
//        QueryWrapper<ICustomers> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("i_name",orderForm.getCustomerName());
//        ICustomers iCustomers = iCustomersMapper.selectOne(queryWrapper);
////        System.out.println(iCustomers.getIName());
//
//        QueryWrapper<ECustomers> queryWrapper2 = new QueryWrapper<>();
//        queryWrapper.eq("e_name",orderForm.getCustomerName());
//        ECustomers eCustomers = eCustomersMapper.selectOne(queryWrapper2);
     //   System.out.println(eCustomers.getEName());

//        if (StringUtils.isNotBlank(iCustomers.getIName())){
//            if (StringUtils.isNotBlank(eCustomers.getEName())){
//                return RespResult.error("客户不存在");
//            }
//        }

//        try {
//            if (! StringIsNotNull.checkObjFieldIsNull(iCustomers.getIName())){
//                if (!StringIsNotNull.checkObjFieldIsNull(eCustomers.getEName())){
//                    return RespResult.error("客户不存在");
//                }
//            }
//
//        } catch (IllegalAccessException e) {
//            throw new RuntimeException(e);
//        }


        orderForm.setOrderStatus(IndentState.REVIEWED);
        log.info("生成订单号："+OddNumbersUtils.creatON("HY"));
        orderForm.setOrderNumber(OddNumbersUtils.creatON("HY"));
        int i = indentMapper.insert(orderForm);
        log.info("添加完成，添加条数为："+i);
        if (i<=0){
            return RespResult.error("添加失败");
        }
        else {
            return RespResult.success();
        }
    }

    @Override
    public RespResult delIndent(Integer[] ids) {
        log.info("进入删除方法，要删除的id号："+ids);
        int i = indentMapper.deleteBatchIds(Arrays.asList(ids));
        log.info("删除的条数="+i);
        return RespResult.success();
    }

    @Override
    public RespResult updateIndent(OrderForm orderForm) {
        log.info("进入修改方法，要修改的订单信息："+orderForm);
        int i = indentMapper.updateById(orderForm);
        log.info("修改完成，条数="+i);
        return RespResult.success();
    }

    @Override
    public RespResult processIndent(Integer id) {
        log.info("进入审核方法");
        OrderForm orderForm = indentMapper.selectById(id);
        orderForm.setOrderStatus(IndentState.UNAUDITED);
        int i = indentMapper.updateById(orderForm);
        log.info("审核通过："+orderForm);
        return RespResult.success();
    }

    @Override
    public RespResult cancelIndent(Integer id) {
        log.info("进入订单作废方法");
        OrderForm orderForm = indentMapper.selectById(id);
        orderForm.setOrderStatus(IndentState.CANCEL);
        int i = indentMapper.updateById(orderForm);
        log.info("该订单已作废："+orderForm);
        return RespResult.success();
    }

    @Override
    public RespResult showIndentById(Integer id) {
        log.info("进入id查询方法");
        OrderForm orderForm = indentMapper.selectById(id);
        log.info("查询结果："+orderForm);
        return RespResult.success(orderForm);
    }
}
